package com.fighthouse.entity;

import java.io.Serializable;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@NamedQueries({
	@NamedQuery(name="menuitens.getMenuItens", query="select men from Item men where men.pai = null and tipo <> :tipo"),
	@NamedQuery(name="menuitens.getMenuItensInvisible", query="select men from Item men where men.pai = null and tipo = :tipo")
})
@Table(name="menuitens")
public class Item implements Serializable, Comparable<Item>{
	private static final long serialVersionUID = 1L;
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;
	
	@Column(length=150)
	private String referencia;
	
	@ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="idPai")
	private Item pai;
	
	@OneToMany(mappedBy="menuItem",cascade=CascadeType.ALL,fetch=FetchType.EAGER)
	private Set<Conteudo> conteudos;
	
	@OneToMany(mappedBy="pai",cascade=CascadeType.ALL,fetch=FetchType.EAGER)
	private Set<Item> submenu;
		
	@Column(length=255)
	private String url;
	
	@Column
	private int tipo;
	
	public int compareTo(Item item){
		int retorno = 0;//caso de igualdade
		
		if(this.id < item.getId())retorno = -1;
		if(this.id > item.getId())retorno = 1;
		
		return retorno;
		
	}

	public void addConteudo(Conteudo conteudo){
		if(conteudos == null) conteudos = new LinkedHashSet<Conteudo>();
		conteudo.setMenuItem(this);
		conteudos.add(conteudo);
	}
	
	public void addSubMenuItem(Item menuItem){
		if(submenu == null) submenu = new LinkedHashSet<Item>();
		menuItem.setPai(this);
		submenu.add(menuItem);
	}
	
	
@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result
				+ ((referencia == null) ? 0 : referencia.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Item other = (Item) obj;
		if (id != other.id)
			return false;
		if (referencia == null) {
			if (other.referencia != null)
				return false;
		} else if (!referencia.equals(other.referencia))
			return false;
		return true;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getReferencia() {
		return referencia;
	}

	public void setReferencia(String referencia) {
		this.referencia = referencia;
	}

	public Item getPai() {
		return pai;
	}

	public void setPai(Item pai) {
		this.pai = pai;
	}

	public Set<Conteudo> getConteudos() {
		return conteudos;
	}

	public void setConteudos(Set<Conteudo> conteudos) {
		this.conteudos = conteudos;
	}

	public Set<Item> getSubmenu() {
		return submenu;
	}

	public void setSubmenu(Set<Item> submenu) {
		this.submenu = submenu;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int getTipo() {
		return tipo;
	}

	public void setTipo(int tipo) {
		this.tipo = tipo;
	}

	
	
}
